package com.android.videotest.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.viewpager.widget.ViewPager;

import com.android.videotest.R;
import com.android.videotest.Urls;
import com.android.videotest.base.BaseActivity;
import com.android.videotest.view.banner.BannerView;
import com.bumptech.glide.Glide;
import com.sansecy.android.simplevideo.SimpleVideoView;

import java.util.ArrayList;

public class BannerActivity extends BaseActivity {
    private static final String TAG = "BannerActivity-App";

    public static void start(Context context) {
        Intent starter = new Intent(context, BannerActivity.class);
//    starter.putExtra();
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);

        ArrayList<BannerItem> bannerItems = new ArrayList<>();
        bannerItems.add(new BannerItem("http://pics1.baidu.com/feed/c8ea15ce36d3d5391128e6aa051c7155342ab0e7.jpeg?token=d9c3b5563e676b813923348a250b32c2&s=B3B65A815C7392DA4C25748E0300A082"));
        bannerItems.add(new BannerItem("http://pics7.baidu.com/feed/5d6034a85edf8db17bc052ab32b84551544e74b7.jpeg?token=ab20dbaa3c90050fe1f351acb613462f&s=B7B25E814CD39ECE361109340300E0C2"));
        bannerItems.add(new BannerItem("", "http://www.sansecy.com/downloader/download/20191028104102_%E6%9D%A1%E7%A0%81%E7%A7%A4%E8%A7%84%E5%88%99.mp4"));
        bannerItems.add(new BannerItem("", "http://www.sansecy.com/downloader/download/test_vide.mp4"));
        bannerItems.add(new BannerItem("http://i0.hdslb.com/bfs/article/9733aa7424e13b0d16572e2fdfda987c38ef9b42.jpg"));
        bannerItems.add(new BannerItem("", "http://video.htd.cn/sv/2e1d7394-16e6824c18c/2e1d7394-16e6824c18c.mp4"));
        bannerItems.add(new BannerItem("https://img.52z.com/upload/news/image/20180621/20180621055734_59936.jpg"));

        BannerView bannerView = findViewById(R.id.banner);
        BannerView.AbsPagerAdapter<BannerItem> adapter = new BannerView.AbsPagerAdapter<BannerItem>(bannerView) {

            @Override
            protected View instantiateItemView(ViewGroup container, int position) {
                BannerItem item = getItem(position);
                View view;
                if (TextUtils.isEmpty(item.url)) {
                    SimpleVideoView videoView = new SimpleVideoView(mContext);
                    videoView.setTag(position);
                    videoView.loadUrl(Urls.cacheVideoUrl(item.VidelUrl));
                    videoView.setOnCompletionListener((mp) -> {
                        //播放完成，下个视频
                        bannerView.postDelayed(bannerView::startAutoScroll, 300);
                    });
                    videoView.setOnErrorListener((mp,what, extra) -> {
                        //出错，跳过此视频
                        bannerView.postDelayed(bannerView::startAutoScroll, 300);
                        return true;
                    });
                    videoView.setLayoutParams(new ViewGroup.MarginLayoutParams(-1, -1));
                    view = videoView;
                } else {
                    ImageView imageView = new ImageView(mContext);
                    imageView.setLayoutParams(new ViewGroup.MarginLayoutParams(-1, -1));
                    Glide.with(mContext).load(item.url).into(imageView);
                    view = imageView;
                }
                return view;
            }

            @Override
            protected void destroyItemView(ViewGroup view, int position, Object object) {
                view.removeView((View) object);
            }
        };

        bannerView.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_DRAGGING) {
                    bannerView.stopAutoScroll();
                }
            }

            @Override
            public void onPageSelected(int position) {
                handleVideoPageSelected(bannerView, position);
            }
        });

        bannerView.setAdapter(adapter);
        adapter.setData(bannerItems);
        bannerView.startDelayAutoScroll();
    }

    private void handleVideoPageSelected(BannerView bannerView, int position) {
        for (int i = 0; i < bannerView.getChildCount(); i++) {
            View child = bannerView.getChildAt(i);
            Object tag = child.getTag();
            //带有position标记的视图是HtdSimpleVideoView
            if (tag instanceof Integer) {
                int intTag = (int) tag;
                if (intTag == position && playVideo != position) {
                    Log.d(this.getClass().getName(), "onPageSelected: ");
                    bannerView.stopAutoScroll();
                    if (child instanceof SimpleVideoView) {
                        SimpleVideoView videoView = (SimpleVideoView) child;
                        if (!videoView.isPlaying()) videoView.openVideo();
                    }
                } else {
                    if (child instanceof SimpleVideoView) {
                        ((SimpleVideoView) child).stopPlayback();
                    }
                }
            }
        }
        playVideo = position;
    }

    private int playVideo = -1;

    class BannerItem {
        private String url;
        private String VidelUrl;

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getVidelUrl() {
            return VidelUrl;
        }

        public void setVidelUrl(String videlUrl) {
            VidelUrl = videlUrl;
        }

        public BannerItem(String url) {
            this.url = url;
        }

        public BannerItem(String url, String videlUrl) {
            this.url = url;
            VidelUrl = videlUrl;
        }
    }
}

